package stockscreener.persistencia;

import java.util.Date;
import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import stockscreener.negocio.BalancoPatrimonial;
import stockscreener.negocio.Empresa;
import stockscreener.util.StockScreenerApplicationContext;
import stockscreener.util.UtilData;

public class BalancoPatrimonialDAO extends HibernateDaoSupport {

	public void salva(BalancoPatrimonial bp) {

		HibernateTemplate ht = getHibernateTemplate();
		ht.saveOrUpdate(bp);
	}

	public void salva(List<BalancoPatrimonial> listaBalancos) {

		HibernateTemplate ht = getHibernateTemplate();

		for (BalancoPatrimonial bp : listaBalancos) {
			ht.save(bp);
		}
	}

	public List<BalancoPatrimonial> obterPorEmpresaEmOrdemDecrescenteDeData(Empresa empresa) {
		HibernateTemplate ht = getHibernateTemplate();
		return ht.find("From BalancoPatrimonial as balancoPatrimonial where balancoPatrimonial.empresa = " + empresa.getId() + " order by balancoPatrimonial.data desc");
	}
	
	public List<BalancoPatrimonial> obtemPorDataEEmpresa(Date data, Empresa empresa) {
		HibernateTemplate ht = getHibernateTemplate();
		return ht.find("From BalancoPatrimonial as balancoPatrimonial where balancoPatrimonial.data = '" + UtilData.formatarData(data, "yyyy-MM-dd") + "' and balancoPatrimonial.empresa = " + empresa.getId());
	}
	
	public List<BalancoPatrimonial> obtemTodos() {
		HibernateTemplate ht = getHibernateTemplate();
		return ht.find("From BalancoPatrimonial");
	}
	
	public static BalancoPatrimonialDAO get() {
		return (BalancoPatrimonialDAO) StockScreenerApplicationContext
				.getBean(BalancoPatrimonialDAO.class);
	}
}
